package chapter07;

import java.util.*;

public class Test04_hashMap {
    public static void main(String[] args) {
        HashMap hashMap = new HashMap();
        //增加元素
        hashMap.put(1,"张三");
        hashMap.put(3,"李四");
        hashMap.put(2,"王五");
        hashMap.put(5,"赵六");
        System.out.println(hashMap);
        //查询
        //可以直接取key值
        System.out.println(hashMap.get(1));
        //可以判断对应的值是否存在
        System.out.println(hashMap.containsKey(2));
        System.out.println(hashMap.containsValue("王五"));
        //修改 直接加入重复的键 即可覆盖原来的value
        hashMap.put(1,"田七");
        System.out.println(hashMap);
        //删除 直接删除对应的键值
        hashMap.remove(1);
        System.out.println(hashMap);
        //遍历输出
        Set set = hashMap.entrySet();
        Iterator iterator = set.iterator();
        while(iterator.hasNext()){
            Map.Entry next = (Map.Entry)iterator.next();
            System.out.println(next.getKey());
            System.out.println(next.getValue());
        }
        //使用key遍历
        Set set1 = hashMap.keySet();
        Iterator iterator1 = set1.iterator();
        while(iterator1.hasNext()){
            Object next = iterator1.next();
            System.out.println(next);
            System.out.println(hashMap.get(next));
        }
        //统计字符串中字符出现的个数
    }
}
